package ch.unisi.inf.editor;

import java.awt.Color;
import ch.unisi.inf.engine.Image;


/**
 * subclass of the ImageInfo class, gets the saturation of an image
 * 
 * @author  matthias.eberli@lu.unisi.ch
 */
public class InfoSaturation extends ImageInfo
{
    public InfoSaturation(final String name)
    {
        super(name);
    }
    
    public int getInfo(final Image image)
    {
        int saturation = 0;
        final int height = image.getHeight();
        final int width = image.getWidth();
        for(int y = 0; y < height; y++) {
            for(int x = 0; x < width; x++) {
                Color pixel = image.getPixel(x, y);
                saturation += (getMax(pixel) - getMed(pixel))+(getMed(pixel) - getMin(pixel))/255 * 100;
            }
        }
        saturation = saturation / (height * width);
        return saturation;
    }
    
    private int getMax(final Color color)
    {
        final int red = color.getRed();
        final int green = color.getGreen();
        final int blue = color.getBlue();
        
        if(red > green && red > blue){
            return red;
        }
        if(green >= red && green >= blue){
            return green;
        }
        else{
            return blue;
        }
    }
        
     
     private int getMed(final Color color)
     {
        final int red = color.getRed();
        final int green = color.getGreen();
        final int blue = color.getBlue();
        
        if(red > green && red < blue){
            return red;
        }
        if(green >= red && green <= blue){
            return green;
        }
        else{
            return blue;
        }
     }
     
     private int getMin(final Color color)
     {
        final int red = color.getRed();
        final int green = color.getGreen();
        final int blue = color.getBlue();
        
        if(red < green && red < blue){
            return red;
        }
        if(green <= red && green <= blue){
            return green;
        }
        else{
            return blue;
        }
     }  
}
